package com.example.demo.thread;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.*;

/**
 * 局部线程池
 *
 * @author Luodl
 */
public class Demo9 {

    public static void main(String[] args) {
        tt(1);
        System.out.println("111111");

        /*while (true) {
            try {
                ExecutorService service = Executors.newFixedThreadPool(1);
                service.submit(new Runnable() {
                    public void run() {
                        try {
                            Thread.sleep(2000); //模拟处理业务
                        } catch (InterruptedException e) {
                        }
                    }
                });
                service = null;
            } catch (Exception e) {
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }*/

        /*while (true) {
            ExecutorService service = Executors.newFixedThreadPool(1);
            try {
                service.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                        }
                    }
                });
            } catch (Exception e) {
            } finally {
                service.shutdown();
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }*/

        /*RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
        String jvmName = runtimeBean.getName();
        System.out.println("JVM Name = " + jvmName);
        long pid = Long.valueOf(jvmName.split("@")[0]);
        System.out.println("JVM PID  = " + pid);
        ThreadMXBean bean = ManagementFactory.getThreadMXBean();
        int n = 1000;
        for (int i = 0; i < n; i++) {
            ThreadPoolExecutor executor = new ThreadPoolExecutor(
                    10, 20, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>());
            //executor.allowCoreThreadTimeOut(true);
            for (int j = 0; j < 5; j++) {
                executor.execute(() -> {
                    System.out.println("当前线程总数为：" + bean.getThreadCount());
                });
            }
            executor.shutdown();
        }
        try {
            Thread.sleep(10000);
        } catch (Exception e) {
        }
        System.out.println("线程总数为 = " + bean.getThreadCount());*/
    }

    public static void tt(int loopCounter) {
        while (loopCounter < 100) {
            try {
                loopCounter++;
                System.out.println(loopCounter);
                ExecutorService service = Executors.newFixedThreadPool(1);
                service.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                        }
                    }
                });
                service = null;
            } catch (Exception e) {
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }
    }
}
